# -*- coding: utf-8 -*-
"""
Created on Mon May 31 09:39:42 2021

@author: huangyue
"""
import pandas as pd
# import matplotlib.pyplot as plt

# import pymssql

# %%


def get_existedinfo(cursor, variable_prefix, begdate=None, enddate=None, useenddate = False):
    # 提取历史上的模型拟合数据
    if useenddate:
        if (begdate==enddate) | (begdate==None):
            str_existedinfo = '''
                            select * from FI_SeriesIndicators
                            where Indicator like '%s_%%'
                            and KeyDate <= '%s'
                            ''' %(variable_prefix,enddate)
        else:
            str_existedinfo = '''
                            select * from FI_SeriesIndicators
                            where Indicator like '%s_%%'
                            and KeyDate>='%s' and KeyDate <= '%s'
                            ''' %(variable_prefix,begdate,enddate)
    else:
        # enddate当天的数据不提取
        if (begdate==enddate) | (begdate==None):
            str_existedinfo = '''
                            select * from FI_SeriesIndicators
                            where Indicator like '%s_%%'
                            and KeyDate < '%s'
                            ''' %(variable_prefix,enddate)
        else:
            str_existedinfo = '''
                            select * from FI_SeriesIndicators
                            where Indicator like '%s_%%'
                            and KeyDate>='%s' and KeyDate < '%s'
                            ''' %(variable_prefix,begdate,enddate)
                        
    cursor.execute(str_existedinfo)
    existed_info = pd.DataFrame(cursor.fetchall(),
                                columns=['Indicator','SecuCode','KeyDate','Value'])
    
    return existed_info